package com.gxa.controller.community;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.R;
import com.gxa.common.utils.Result;
import com.gxa.dto.communitydto.HouseOwnerDto;
import com.gxa.entity.community.Buildings;
import com.gxa.entity.community.Community;
import com.gxa.entity.community.House;
import com.gxa.entity.community.House;
import com.gxa.service.community.HouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.gxa.common.utils.R.error;
import static com.gxa.common.utils.R.ok;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-11 10:05:39
 */
@Api(tags = "房屋管理接口")
@RestController
@RequestMapping("/house")
public class HouseController {

    @Autowired
    private HouseService houseService;

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "房屋列表接口")
    @GetMapping("/list")
    public Result<PageInfo> queryAll(House house, Integer pageNum, Integer pageSize){
        PageInfo pageInfo;
        try{
            pageInfo = houseService.queryAll(house, pageNum, pageSize);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查询失败！");
        }
        return Result.success(pageInfo);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "新增房屋接口")
    @PostMapping("/add")
    public Result add(@RequestBody House house){
        try{
            houseService.add(house);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("添加失败！");
        }
        return Result.success();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "删除房屋接口")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Integer id){
        try{
        houseService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("删除失败！");
        }

        return Result.success();
    }
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "根据id查询房屋接口")
    @GetMapping("/queryById/{id}")
    public Result<House> queryById(@PathVariable("id") Integer id){
        House house;
        try{
            house = houseService.getById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查询失败！");
        }
        return Result.success(house);
    }
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "编辑房屋接口")
    @PutMapping("/edit")
    public Result edit(@RequestBody House house){
        try{
        houseService.update(house);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("修改失败！");
        }
        return Result.success();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "根据房屋的id查询业主列表的接口")
    @GetMapping("/queryByHouseId/{houseNum}")
    public Result<List<HouseOwnerDto>> queryByHouseId(@PathVariable("houseNum") String houseNum){
        List<HouseOwnerDto> houseOwnerDtos;
        try{
            houseOwnerDtos = houseService.queryByHouseId(houseNum);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查询失败！");
        }
        return Result.success(houseOwnerDtos);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "异步请求判断房屋编号是否重复的接口")
    @GetMapping("/judgeOne")
    public Result judgeOne(String houseNum){
        List<House> houses = houseService.queryByNum(houseNum);
        if (houses.size() != 0) {
            return Result.failed("房屋编码重复了，请重新输入");
        }else {
            return Result.success();
        }
    }
}
